package com.sttk.secreconeclick.bookkeep.adapter;

import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.ProgressBar;

import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.lzb.basic.ProviderMultiAdapter;
import com.sttk.secreconeclick.bookkeep.R;
import com.sttk.secreconeclick.bookkeep.bean.BillBean;
import com.sttk.secreconeclick.bookkeep.bean.PlanBean;
import com.sttk.secreconeclick.bookkeep.bean.SaveRecordBean;

import org.jetbrains.annotations.NotNull;

import java.text.SimpleDateFormat;


public class PlanAdapter extends ProviderMultiAdapter<PlanBean> {

    public PlanAdapter() {
        addItemProvider(new PlanItemProvider());
    }

    private class PlanItemProvider extends BaseItemProvider<PlanBean> {
        @Override
        public int getItemViewType() {
            return ProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_plan;
        }

        @Override
        public void convert(@NotNull BaseViewHolder baseViewHolder, PlanBean bean) {
            baseViewHolder.setText(R.id.tvPlanItemName, bean.getName());

            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            long now = TimeUtils.string2Millis(TimeUtils.getNowString(format), format);
            long date = 0L;
            if (now >= bean.getStartDate()) {
                date = (bean.getEndDate() - now) / (1000 * 60 * 60 * 24); // 转换为天数
                baseViewHolder.setText(R.id.tvPlanItemDate, context.getString(R.string.until_end, date));
            } else {
                date = (bean.getStartDate() - now) / (1000 * 60 * 60 * 24); // 转换为天数
                baseViewHolder.setText(R.id.tvPlanItemDate, context.getString(R.string.until_start, date));
            }

            double saveMoney = 0;
            for (SaveRecordBean saveRecordBean : bean.getRecordBeanList()) {
                if (saveRecordBean.isSave()) {
                    saveMoney += saveRecordBean.getMoney();
                } else {
                    saveMoney -= saveRecordBean.getMoney();
                }
            }
            ProgressBar progressBar = baseViewHolder.getView(R.id.pbPlanItemProgress);
            if (saveMoney < 0) {
                progressBar.setProgress(0);
            } else {
                progressBar.setProgress((int) (saveMoney / bean.getBudget() * 100));
            }
            baseViewHolder.setText(R.id.tvPlanItemSaveMoney, String.format("%.2f", saveMoney));
            baseViewHolder.setText(R.id.tvPlanItemBudget, String.format("%.2f", bean.getBudget()));
        }
    }
}
